package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SendCodeService;
import com.itheima.health.vo.SmsLoginParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: liangWeiQUQ
 * @Description: 手机通过验证码登录
 * @DateTime: 2021/3/6 8:48
 **/
@RestController
@Slf4j
@RequestMapping("mobile/login")
public class LoginController {
    @DubboReference
    private SendCodeService sendCodeService;
    @DubboReference
    private MemberService memberService;

    /**
       * @Author: 传闻中的梁大侠
       * @Date: 8:56 2021/3/6
       * @Parms [smsLoginParam]
       * @ReturnType: com.itheima.health.entity.Result
       * @Description: 短信验证用户登录
     */
    @PostMapping("smsLogin")
    public Result login(@RequestBody SmsLoginParam smsLoginParam) {
        //验证码核对校验
        boolean success = sendCodeService.checkCode("MOBILE_SIGNIN", smsLoginParam.getTelephone(), smsLoginParam.getValidateCode());
        if (!success) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        //手机号登录的直接创建用户
        Member member = new Member();
        member.setPhoneNumber(smsLoginParam.getTelephone());
        member = memberService.addIfNotExist(member);
        //登录（模拟，直接输出成功）
        log.info("[登录成功]》》》》》》》》》》》》》》》》》》》》》》》》{}", member.getId());
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
}
